package com.adv.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;

import org.dozer.Mapper;

import com.adv.model.Message;

/**
 * 对象转换类
 * 
 * @author WUQINGLONG
 *
 */
public class DozerUtil {

	/**
	 * 
	 * @Title: mapObj
	 * @Description: 对象转换到指定类
	 * @param form
	 * @param target
	 * @return
	 * @return: T
	 */
	public static <E, T> T mapObj(E form, Class<T> target) {
		if (null != form) {
			Mapper baseMapper = SpringContextHolder.getBean("baseMapper", Mapper.class);
			return baseMapper.map(form, target);
		}
		return null;
	}

	/**
	 * 
	 * @Title: mapObj
	 * @Description: 向指定对象填充属性
	 * @param form
	 * @param target
	 * @return: void
	 */
	public static <E, T> void mapObj(E form, T target) {
		if (null != form) {
			Mapper baseMapper = SpringContextHolder.getBean("baseMapper", Mapper.class);
			baseMapper.map(form, target);
		}
	}

	/**
	 * 
	 * @Title: mapList
	 * @Description: 集合对象转换
	 * @param sourceList
	 * @param targetCls
	 * @return
	 * @return: List<T>
	 */
	public static <T> List<T> mapList(Collection<?> sourceList, Class<T> targetCls) {
		Mapper baseMapper = SpringContextHolder.getBean("baseMapper", Mapper.class);
		List<T> result = new ArrayList<T>();
		if (null != sourceList && !sourceList.isEmpty()) {
			for (Iterator<?> it = sourceList.iterator(); it.hasNext();) {
				T target = baseMapper.map(it.next(), targetCls);
				result.add(target);
			}
		}
		return result;
	}

	public static void main(String[] args) {
		mapObj(null, Message.class);
	}

}
